from datetime import datetime
from flask_login import UserMixin
from sqlalchemy import DateTime, Numeric
from werkzeug.security import generate_password_hash, check_password_hash
from applications.exts import db


class Orders(db.Model):
    __tablename__ = 'wx_orders'
    oid = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='订单的id')
    openid = db.Column(db.String(30), db.ForeignKey('wx_user.openid'), nullable=False, comment='小程序的用户openid')
    order_sn = db.Column(db.String(44), nullable=False, comment='订单编号')
    goodsid = db.Column(db.Integer, db.ForeignKey('wx_goods.id'), comment='商品表的外件')
    # num=db.Column(db.Integer,default=1,comment='订单数量')
    redatetime = db.Column(db.DateTime, default=datetime.now(), comment='订单创建时间')
    pay_type = db.Column(db.String(4), nullable=False, default=0, comment='支付方式')
    order_price = db.Column(db.Float, nullable=False, default=0.00, comment='订单价格')
    yuan_price = db.Column(db.Float, nullable=False, default=0.00, comment='支付价格')
    order_status = db.Column(db.Integer, nullable=False, default=0, comment='订单状态')
    code = db.Column(db.String(10), nullable=False, comment='取货码')
    pay_status = db.Column(db.Integer, nullable=False, default=0, comment='支付状态')
    send_status = db.Column(db.Integer, nullable=False, default=0, comment='发货状态')
    send_type = db.Column(db.Integer, nullable=False, default=0, comment='配送方式0店内，1外卖')
    send_sn = db.Column(db.String(40), nullable=False, comment='物流单号')
    store=db.Column(db.String(30),default='',comment='商店')
    addressid = db.Column(db.Integer, db.ForeignKey('wx_user_address.id'), comment='收货地址')

    # wxuid = db.Column(db.Integer, db.ForeignKey('user.id'), comment='用户表的外件')

    # orders = db.relationship('Goods', backref='order')



    def __str__(self):
        return self.oid

    def to_dict(self):
        res = {}
        for col in self.__table__.columns:
            if isinstance(col.type, DateTime):  # 判断类型是否为DateTime
                if not getattr(self, col.name):  # 判断实例中该字段是否有值
                    value = ""
                else:  # 进行格式转换
                    value = getattr(self, col.name).strftime("%Y-%m-%d %H:%M:%S")
            elif isinstance(col.type, Numeric):  # 判断类型是否为Numeric
                value = float(getattr(self, col.name))  # 进行格式转换
            else:  # 剩余的直接取值
                value = getattr(self, col.name)
            res[col.name] = value
        return res


class Orders_good_item(db.Model):
    __tablename__ = 'wx_orders_item'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='订单的id')
    order_sn = db.Column(db.String(44), db.ForeignKey('wx_user.openid'), comment='订单编号')
    goodsid = db.Column(db.Integer, db.ForeignKey('wx_goods.id'), comment='商品表的外件')
    number= db.Column(db.Integer,nullable=False,comment='商品的购买数量')

    def __str__(self):
        return self.number

    def to_dict(self):
        res = {}
        for col in self.__table__.columns:
            if isinstance(col.type, DateTime):  # 判断类型是否为DateTime
                if not getattr(self, col.name):  # 判断实例中该字段是否有值
                    value = ""
                else:  # 进行格式转换
                    value = getattr(self, col.name).strftime("%Y-%m-%d %H:%M:%S")
            elif isinstance(col.type, Numeric):  # 判断类型是否为Numeric
                value = float(getattr(self, col.name))  # 进行格式转换
            else:  # 剩余的直接取值
                value = getattr(self, col.name)
            res[col.name] = value
        return res
